package me.monster.smarttraffic.base;

import androidx.annotation.StringRes;

import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import me.monster.smarttraffic.tool.StringUtil;
import me.monster.smarttraffic.tool.Util;

/**
 * @description
 * @author: Created jiangjiwei in 2018/11/2 09:36
 */
public class BasePresenter<T extends IView> implements IPresenter<T> {

    public T iView;
    private CompositeDisposable mDisposableManager;

    @Override
    public void attachView(T View) {
        this.iView = View;
    }

    @Override
    public void detachView() {
        onDestroy();
        this.iView = null;
    }

    private void onDestroy() {
        if (mDisposableManager != null) {
            mDisposableManager.dispose();
        }
    }

    public void addDispose(Disposable disposable) {
        if (mDisposableManager == null) {
            mDisposableManager = new CompositeDisposable();
        }
        mDisposableManager.add(disposable);
    }

    public String append(Object... objects) {
        return StringUtil.append(objects);
    }

    public String handleError(Throwable throwable) {
        throwable.printStackTrace();
        return throwable.getMessage();
//        return ErrorHanding.getInstance().handError(throwable);
    }

    /**
     * 从 字符串文件格式化 字符
     *
     * @param resId      字符串 id
     * @param formatArgs formatArgs
     * @return string
     */
    public final String getString(@StringRes int resId, Object... formatArgs) {
        return Util.getTopActivityOrApp().getString(resId, formatArgs);
    }

    public final String getString(@StringRes int resId) {
        return Util.getTopActivityOrApp().getString(resId);
    }
}
